home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 103 / CD-ROM 103.iso / edu / martianwin / src / gfx.h < prev    next >
Encoding:
Text File  |  2003-08-11  |  1.5 KB  |  51 lines

  1. /*
  2.     LoadT8 carga un grafico en formato .T8 de 8 bits en una superficie SDL
  3. */
  4. SDL_Surface * T_LoadT8(char *str)
  5. {
  6.     SDL_Surface *Tsurface;
  7.     FILE *Tsrc;
  8.     Tsrc=fopen(str, "rb");
  9.     if ( Tsrc == NULL)
  10.     {
  11.         fprintf(stderr, "can't read from file \n");
  12.         SDL_Quit();
  13.     exit(0);
  14.     }
  15.     // leer tama±o del bmp
  16.     fseek(Tsrc,6,SEEK_SET); // Saltamos la cabecera "T-1000"
  17.     // leemos tama±o del bitmap
  18.     int wh=getc(Tsrc);    int wl=getc(Tsrc);    int hh=getc(Tsrc);    int hl=getc(Tsrc);
  19.     int width=wh*256+wl;    int height=hh*256+hl;
  20.     // leemos la paleta
  21.     int paleta[768];
  22.     for(int pal=0;pal<768;pal++)
  23.         paleta[pal]=getc(Tsrc);
  24.     // leemos el bitmap en una superficie de 24 bits
  25.     SDL_Surface *temp;
  26.     temp = SDL_CreateRGBSurface(SDL_SWSURFACE,width,height,24,0,0,0,0);
  27.     if(SDL_MUSTLOCK(temp))
  28.     SDL_LockSurface(temp);
  29.     Uint32 pixel;
  30.     int x,y,color;
  31.     for(y=0;y<height;y++) //el tama±o del grafico es de ancho * alto * 3 (24 bits)
  32.     {
  33.     for(x=0;x<width;x++)
  34.     {
  35.         color=getc(Tsrc);
  36.             pixel = SDL_MapRGB(temp->format,paleta[color*3],paleta[(color*3)+1],paleta[(color*3)+2]);
  37.         Uint8 *p = (Uint8 *)temp->pixels + (y) * temp->pitch + (x) * 3;
  38.         p[0] = pixel & 0xff;
  39.         p[1] = (pixel >> 8) & 0xff;
  40.         p[2] = (pixel >> 16) & 0xff;
  41.         }
  42.     }
  43.     if(SDL_MUSTLOCK(temp))
  44.     SDL_UnlockSurface(temp);
  45.     Tsurface = SDL_DisplayFormat(temp);
  46.     SDL_FreeSurface(temp);
  47.     fclose(Tsrc);
  48.     return(Tsurface);
  49. }
  50.  
  51.